package controller

import (
	"github.com/gin-gonic/gin"
	"go-demo/common"
	"go-demo/model"
	"go-demo/service"
	"net/http"
	"strconv"
)

type ChatResponse struct {
	model.Response
	MessageList []model.MessageSendEvent `json:"message_list"`
}

func MessageAction(c *gin.Context) {
	token := c.Query("token")
	toUserId, _ := strconv.ParseInt(c.Query("to_user_id"), 10, 64)
	content := c.Query("content")
	actionType := c.Query("action_type")
	messageSendEvent := service.CreateMessageSendEvent(token, toUserId, actionType, content)
	if messageSendEvent != nil {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.SUCCESS, StatusMsg: "发送消息/接收消息成功"})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "发送消息/接收消息失败"})
	}
}

func MessageChat(c *gin.Context) {
	token := c.Query("token")
	toUserId, _ := strconv.ParseInt(c.Query("to_user_id"), 10, 64)
	messageList := service.CreateMessageChat(token, toUserId)

	if messageList != nil {
		c.JSON(http.StatusOK, ChatResponse{Response: model.Response{StatusCode: common.SUCCESS, StatusMsg: "获取消息列表成功"}, MessageList: messageList.([]model.MessageSendEvent)})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "获取消息列表失败"})
	}
}
